unit fmCustomEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxContainer, cxEdit, dxSkinsCore, dxSkinOffice2007Black, AdvGlowButton,
  cxLabel, cxGroupBox, AdvReflectionLabel, StdCtrls, GradientLabel, Menus,
  cxButtons, Uni, ActnList, DB, ImgList;

type
  TCustomEditForm = class(TForm)
    glbCardCaption: TGradientLabel;
    lblCardCaption: TAdvReflectionLabel;
    grpButtons: TcxGroupBox;
    btnApply: TcxButton;
    btnClose: TcxButton;
    pnlAll: TcxGroupBox;
    CustomEditActionList: TActionList;
    actApply: TAction;
    actClose: TAction;
    il16: TcxImageList;
    procedure actApplyUpdate(Sender: TObject);
    procedure actCloseExecute(Sender: TObject);
    procedure actApplyExecute(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  protected
    FDataSource: TUniDataSource;  
    FDataChanged, FSaved: boolean;
    procedure SetDataSource; virtual;
    procedure SetCaption; virtual;
  public
    { Public declarations }
    class function Execute(AOwner: TComponent; ADataSource: TUniDataSource = nil): boolean;
  end;

implementation

{$R *.dfm}

{ TCustomEditForm }

class function TCustomEditForm.Execute(AOwner: TComponent;
  ADataSource: TUniDataSource): boolean;
begin
  with Create(AOwner) do
  try
    FDataChanged := False;
    FSaved := False;
    FDataSource := ADataSource;
    SetDataSource;
    SetCaption;
    ShowModal;
    Result := FSaved;
  finally
    Free;
  end;
end;

procedure TCustomEditForm.actApplyUpdate(Sender: TObject);
begin
  if actApply.Enabled <> (FDataChanged and (FDataSource.DataSet.State in [dsEdit, dsInsert])) then
    actApply.Enabled := (FDataChanged and (FDataSource.DataSet.State in [dsEdit, dsInsert]));
end;

procedure TCustomEditForm.actCloseExecute(Sender: TObject);
begin
  if FDataSource.DataSet.State in [dsEdit, dsInsert] then
    FDataSource.DataSet.Cancel;
  Close;
end;

procedure TCustomEditForm.SetDataSource;
begin
//
end;

procedure TCustomEditForm.SetCaption;
begin
//
end;

procedure TCustomEditForm.actApplyExecute(Sender: TObject);
begin
  FDataSource.DataSet.Post;
  FDataChanged := False;
  FSaved := True;
end;

procedure TCustomEditForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #27 then
    ModalResult := mrCancel;
end;

end.
